/**
 * @description 所有人可使用的参数配置列表
 * @params hideMenu: 是否隐藏当前路由结点不在导航中展示
 * @params alwayShow: 只有一个子路由时是否总是展示菜单，默认false
 */
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router'
import store from '@/store'
import i18n from '@/locale'
import NProgress from '@/utils/system/nprogress'
import { changeTitle } from '@/utils/system/title'

// 动态路由相关引入数据
import Layout from '@/layout/index.vue'
import MenuBox from '@/components/menu/index.vue'
import { createNameComponent } from './createNode'

// 引入modules
import Dashboard from './modules/dashboard'
import Document from './modules/document'
import Pages from './modules/pages'
import Menu from './modules/menu'
import Component from './modules/component'
import Directive from './modules/directive'
import SystemManage from './modules/systemManage'
import Chart from './modules/chart'
import Print from './modules/print'
import Community from './modules/community'
import System from './modules/system'

// 业务路由 管理员路由
import ServiceRouting from './modules/serviceRouting'

// 初始化必须要的路由
let modules: object[] = [
  ...System
]

const { t } = i18n.global

const routes: any = modules

const router = createRouter({
  history: createWebHistory(),//createWebHashHistory(),
  routes,

})

// 登录后动态加入的路由
let asyncRoutes: RouteRecordRaw[] = [
  // 首页
  // ...Dashboard,
  // 业务路由
  // ...ServiceRouting,
  // ...authRouting

  // 通用组件路由
  // ...Document,
  // ...Component,
  // ...Pages,
  // ...Menu,
  // ...Directive,
  // ...Chart,
  // ...SystemManage,
  // ...Print,
  // ...Community,


]
// 动态路由的权限新增，供登录后调用
export async function addRoutes() {
  // 与后端交互的逻辑处理，处理完后异步添加至页面
  // asyncRoutes.forEach(item => {
  //   modules.push(item)
  //   router.addRoute(item)
  // })

  // let info = JSON.parse(localStorage.getItem('LoginInfo'))
  // let authRouting: any = []
  // if (info) { 
  //   let type = info.memberLevelName // 0:超级管理员 1:客服
  //   if (type == 0) {
  //     authRouting = ServiceRouting
  //   } else {

  //   }
  // }
  // router.options.routes = []

  ServiceRouting.forEach((item: any, i: Number) => { // 添加到路由表里面去
    if (item.path == '/backstagerpzzm/notificationMessage' && store.state.user.adminInfo.admin_type > 1) { // 子管理员隐藏通知消息菜单
      item.hideMenu = true
      ServiceRouting.splice(i, 1)
    }
    if (item.path == '/backstagerpzzm/dataManagement' && store.state.user.adminInfo.admin_type > 1) { // 子管理员隐藏数据管理菜单
      item.hideMenu = true
      ServiceRouting.splice(i, 1)
    }
  })

  let data = ServiceRouting
  // let routers = router.getRoutes()
  // console.log(routers)

  // eachData(data, 0) // 匹配本地路由
  data.forEach((item: any) => { // 添加到路由表里面去
    modules.push(item)
    router.addRoute(item)
  })

}

// 重置匹配所有路由的解决方案，todo
function eachData(data: any, type: number) {
  data.forEach(d => {
    if (d.children && d.children.length > 0) {
      if (type === 0) {
        d.component = Layout
      } else {
        d.component = createNameComponent(MenuBox)
      }
      eachData(d.children, type + 1)
    } else {
      /* 组件匹配暂时写死，todo项 */
      // d.component = createNameComponent(() => import('@/views/main/pages/crudTable/index.vue'))
    }
  })
  console.log(data)
}

// 如果你登录了，那么系统才会把路由加入到路由表里面，防止越权访问
if (store.state.user.token.token) {
  addRoutes()
}

const whiteList = ['/backstagerpzzm/login']

router.beforeEach((to, _from, next) => {
  NProgress.start();
  if (store.state.user.token || whiteList.indexOf(to.path) !== -1) {
    to.meta.title ? (changeTitle(to.meta.title)) : ""; // 动态title
    next()
  } else {
    next("/backstagerpzzm/login"); // 全部重定向到登录页
    to.meta.title ? (changeTitle(to.meta.title)) : ""; // 动态title
  }
});

router.afterEach((to, _from) => {
  const keepAliveComponentsName = store.getters['keepAlive/keepAliveComponentsName'] || []
  const name = to.matched[to.matched.length - 1].components.default.name
  if (to.meta && to.meta.cache && name && !keepAliveComponentsName.includes(name)) {
    store.commit('keepAlive/addKeepAliveComponentsName', name)
  }
  NProgress.done();
});

export {
  modules
}

export default router
